﻿using System;
using System.Collections.ObjectModel;
using System.Net;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ViewMaker;

namespace CloudStorageLight.WpfClient
{
    /// <summary>
    /// TreeViewアイテムのベース
    /// </summary>
    public class TreeItem : ViewModelBase
    {
        static readonly string[] SizeSuffixes = { "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
        static string SizeSuffix(Int64 value)
        {
            if (value < 0) { return "-" + SizeSuffix(-value); }
            if (value == 0) { return ""; }

            int mag = (int)Math.Log(value, 1024);
            decimal adjustedSize = (decimal)value / (1L << (mag * 10));

            return string.Format("{0:n1} {1}", adjustedSize, SizeSuffixes[mag]);
        }


        public string Id { get; set; }

        public string Type { get; set; }

        public virtual string Title
        {
            get { return _Title; }
            set
            {
                _Title = value; OnPropertyChanged("Title");
            }
        }
        private string _Title;

        public virtual string Icon
        {
            get { return _Icon; }
            set { _Icon = value; OnPropertyChanged("Icon"); }
        }
        private string _Icon;

        public string Owner {get; set;}
        public DateTime LastModified {get; set;}
        public string Path { get; set; }
        public long Length {get; set;}

        public string FileProtectedMode { get; set; }

        public string AttachmentFileName { get; set; }

        public bool IsExpanded
        {
            get { return _IsExpanded; }
            set { _IsExpanded = value; OnPropertyChanged("IsExpanded"); }
        }
        private bool _IsExpanded;

        public bool IsSelected
        {
            get { return _IsSelected; }
            set { _IsSelected = value; OnPropertyChanged("IsSelected"); }
        }
        private bool _IsSelected;

        public string Url
        {
            get { return BlobDataManager.Instance.GetFilePathUrl(Path); }
        }

        public string SizeString
        {
            get { return SizeSuffix(Length); }
        }
        public string OldTitle { get; set; }

        public bool IsEditing
        {
            get { return _isEditing; }
            set { _isEditing = value; OnPropertyChanged("IsEditing", "IsNotEditing"); }
        }
        private bool _isEditing;

        public bool IsNotEditing
        {
            get { return !_isEditing; }
        }

        public DateTime LocalLastModified { get { return TimeZone.CurrentTimeZone.ToLocalTime(LastModified); } }


        public void RaisePropertyChanged(params string[] names)
        {
            OnPropertyChanged(names);
        }
      }

}
